﻿using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System;

namespace INSAwarsWPF
{
    /// <summary>
    /// Logique d'interaction pour MenuChoix.xaml
    /// </summary>
    public partial class MenuView : Window
    {
        public string[] ListNoms {get; set;}
        public int[] TabIndexCiv { get; set; }

        public MenuView()
        {
            ListNoms = new string[4];
            TabIndexCiv = new int[4];
            InitializeComponent();
            DataContext = this;
        }

        private void comboBoxTaille_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           
            int taille = comboBoxTaille.SelectedIndex;
            switch (taille)
            {
                case 0:
                    comboBoxNbJoueurs.SelectedIndex = 0;
                    comboBoxNbJoueurs.IsEnabled = false;
                    break;
                case 1:
                    comboBoxNbJoueurs.IsEnabled = true;
                    break;
            }
        }

        private void comboBoxNbJoueurs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int taille = comboBoxNbJoueurs.SelectedIndex;
            switch (taille)
            {
                case 0:
                    // listJoueurs update
                    ListNoms[2] = "";
                    //TabIndexCiv[2] = 0;
                    ListNoms[3] = "";
                    //TabIndexCiv[3] = 0;

                    // J3 update
                    labelJ3.IsEnabled = false;
                    textBoxNom3.IsEnabled = false;
                    textBoxNom3.Clear();
                    comboBoxCivi3.SelectedIndex = 0;
                    comboBoxCivi3.IsEnabled = false;

                    // J4 update
                    labelJ4.IsEnabled = false;
                    textBoxNom4.IsEnabled = false;
                    textBoxNom4.Clear();
                    comboBoxCivi4.SelectedIndex = 0;
                    comboBoxCivi4.IsEnabled = false;

                    break;

                case 1:
                    // listJoueurs update
                    ListNoms[3] = "";
                    //ListCivs[3] = "";

                    labelJ3.IsEnabled = true;
                    textBoxNom3.IsEnabled = true;
                    comboBoxCivi3.IsEnabled = true;

                    labelJ4.IsEnabled = false;
                    textBoxNom4.IsEnabled = false;
                    textBoxNom4.Clear();
                    comboBoxCivi4.SelectedIndex = 0;
                    comboBoxCivi4.IsEnabled = false;

                    break;

                case 2:
                    labelJ3.IsEnabled = true;
                    textBoxNom3.IsEnabled = true;
                    comboBoxCivi3.IsEnabled = true;

                    labelJ4.IsEnabled = true;
                    textBoxNom4.IsEnabled = true;
                    comboBoxCivi4.IsEnabled = true;

                    break;
            }

        }

        private void buttonLancer_Click(object sender, RoutedEventArgs e)
        {
            bool i = true;
            bool j = true;
            try
            {
                if (textBoxNom1.Text == "") throw new Exception();
                if (textBoxNom2.Text == "") throw new Exception();
                if (textBoxNom3.Text == "" && labelJ3.IsEnabled) throw new Exception();
                if (textBoxNom4.Text == "" && labelJ4.IsEnabled) throw new Exception();

            } catch (Exception)
                {
                    MessageBox.Show("Fatal error : nom des joueurs non renseigné !");
                    i = false;
                    j = false;
                }
            if(j){
            try
            {

                if (textBoxNom1.Text == textBoxNom2.Text) throw new Exception();
                if (textBoxNom1.Text == textBoxNom3.Text && labelJ3.IsEnabled) throw new Exception();
                if (textBoxNom1.Text == textBoxNom4.Text && labelJ4.IsEnabled) throw new Exception();
                if (textBoxNom2.Text == textBoxNom3.Text && labelJ3.IsEnabled) throw new Exception();
                if (textBoxNom2.Text == textBoxNom4.Text && labelJ4.IsEnabled) throw new Exception();
                if (textBoxNom3.Text == textBoxNom4.Text && labelJ4.IsEnabled) throw new Exception();
            }
            catch(Exception)
            {
                MessageBox.Show("Fatal error : deux ou plusieurs joueurs ne peuvent porter deux noms identitques !");
                i=false;
            }
            }

             if(i)
                    this.Close();
        }

        private void Window_Initialized(object sender, System.EventArgs e)
        {
            /*ListNoms = new string[4];
            ListCivs = new string[4];*/
        }
    }
}
